﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoketPack.Management;
using System.Web;

namespace RoketPack
{
    class WebRender
    {
        /// <summary>
        /// Generates HTML for a particular application.
        /// </summary>
        /// <param name="application"></param>
        /// <returns></returns>
        internal static string GetProductHTML(Application application)
        {
            if (!application.Valid)
                return WebRender.GetUnknownProductHTML();

            // Detect if the product is not available.
            string error = "";
            if (!application.Available)
                error = "<div class='error'>This product is not available at this time.</div>";

            // Generate the header HTML.
            string html = @"
<div class='header'>" + application.Name + @"</div>
<div class='description'>
  " + application.Description + @"
</div>
" + error + @"
";
            if (application.Launchable && System.IO.File.Exists(PathTranslator.Parse(application.LaunchPath)))
            {
                html += "<a class='launch' href='about:launch?repository=" +
                                     HttpUtility.UrlPathEncode(application.Repository.Url) +
                                     "&product=" + application.ID + @"'>Launch Game</a>
";
            }

            html += @"<div class='components'>COMPONENTS</div>
<ul class='component_list'>
";

            // Loop through all of the components.
            foreach (Component c in application.Components.Values)
            {
                if (c.Available)
                {
                    if (c.IsInstalled())
                    {
                        if (c.IsOutOfDate())
                        {
                            if (application.InstallationData != null && application.InstallationData.Component == c)
                            {
                                html += @"
  <li class='disabled'>
    <div class='statusupdate'>INSTALLING<br/>RIGHT NOW</div>
    " + c.Name + @"
  </li>";
                            }
                            else if (application.InstallationData != null)
                            {
                                html += @"
  <li class='disabled'>
    " + c.Name + @"
  </li>";
                            }
                            else
                            {
                                html += @"
  <a href='about:update?repository=" +
                                     HttpUtility.UrlPathEncode(application.Repository.Url) +
                                     "&product=" + application.ID + "&component=" + c.ID + @"'>
    <div class='statusupdate'>CLICK TO<br/>UPDATE</div>
    " + c.Name + @"
  </a>";
                            }
                        }
                        else
                        {
                            if (application.Repository.Url == Manager.OfficialRepository &&
                                application.ID == Manager.RoketPackID)
                            {
                                html += @"
  <li class='disabled'>
    <div class='status'>INSTALLED</div>
    " + c.Name + @"
  </li>";
                            }
                            else
                            {
                                html += @"
  <a href='about:uninstall?repository=" +
                                         HttpUtility.UrlPathEncode(application.Repository.Url) +
                                         "&product=" + application.ID + "&component=" + c.ID + @"'>
    <div class='status'>INSTALLED</div>
    " + c.Name + @"
  </a>";
                            }
                        }
                    }
                    else if (c.Recommended)
                    {
                        html += @"
  <a href='about:install?repository=" +
                                 HttpUtility.UrlPathEncode(application.Repository.Url) +
                                 "&product=" + application.ID + "&component=" + c.ID + @"'>
    <div class='status'>RECOMMENDED</div>
    " + c.Name + @"
  </a>";
                    }
                    else if (application.InstallationData != null && application.InstallationData.Component == c)
                    {
                        html += @"
  <li class='disabled'>
    <div class='statusupdate'>INSTALLING<br/>RIGHT NOW</div>
    " + c.Name + @"
  </li>";
                    }
                    else if (application.InstallationData != null)
                    {
                        html += @"
  <li class='disabled'>
    " + c.Name + @"
  </li>";
                    }
                    else if (application.Repository.Url == Manager.OfficialRepository &&
                             application.ID == Manager.RoketPackID)
                    {
                                html += @"
  <li class='disabled'>
    <div class='statusupdate'>TEMPORARY<br/>INSTALLATION</div>
    " + c.Name + @"
  </li>";
                    }
                    else
                    {
                        html += @"
  <a href='about:install?repository=" +
                                 HttpUtility.UrlPathEncode(application.Repository.Url) +
                                 "&product=" + application.ID + "&component=" + c.ID + @"'>
    <div class='status'></div>
    " + c.Name + @"
  </a>";
                    }
                }
                else
                {
                    if (application.Repository.Url == Manager.OfficialRepository &&
                        application.ID == Manager.RoketPackID)
                    {
                        html += @"
  <li class='disabled'>
    <div class='status'>INSTALLED</div>
    " + c.Name + @"
  </li>";
                    }
                    else
                    {
                        html += @"
  <li class='disabled'>
    <div class='status'></div>
    " + c.Name + @"
  </li>";
                    }
                }
            }

            html += "</ul>";

            if (application.Repository.Url == Manager.OfficialRepository &&
                application.ID == Manager.RoketPackID)
            {
                html += "<p class='component_installation'><strong>Note:</strong> This software can not be uninstalled.</p>";
            }
            else
            {
                html += "<p class='component_installation'><strong>Note:</strong> Select a component to install / uninstall it.</p>";
            }

            html += "<p class='repository'><strong>Repository:</strong> " + application.Repository.Name + "</p>";

            // Return the generated HTML.
            return html;
        }

        /// <summary>
        /// Returns the HTML for an unknown or invalid product.
        /// </summary>
        /// <returns>The HTML for an unknown or invalid product.</returns>
        internal static string GetUnknownProductHTML()
        {
            return @"
<div class='header'>Unknown Product</div>
<div class='description'>
  <p>The selected product could not be found in the database.</p>
</div>
<div class='error'>This product is not available at this time.</div>
";
        }

        /// <summary>
        /// Returns the HTML for an unknown action
        /// </summary>
        /// <returns>The HTML for an unknown action.</returns>
        internal static string GetUnknownActionHTML()
        {
            return @"
<div class='header'>Unknown Request</div>
<div class='description'>
  <p>Your request could not be completed because RoketPack did not understand it.</p>
</div>
<div class='error'>Please try again later.</div>
";
        }

        internal static string GetRepositoryRefreshHTML()
        {
            return @"
<div class='header'>Refresh In Progress</div>
<div class='description'>
  <p>The repositories are currently being refreshed.  This may take up to
     a minute depending on how many repositories are installed.</p>
</div>
<div class='error'>Software installation not available at this time.</div>
";
        }

        internal static string GetRepositoryInitializationHTML()
        {
            return @"
<div class='header'>Initialization In Progress</div>
<div class='description'>
  <p>The repositories are currently being initialized.  This may take up to
     a minute depending on how many repositories are installed.</p>
</div>
<div class='error'>Software installation not available at this time.</div>
";
        }

        internal static string GetDefaultPage()
        {
            return @"
<div class='header'>Select Application</div>
<div class='description'>
  <p>Select an application to view more information or install the product.</p>
</div>";
        }
    }
}
